#include <gtk/gtk.h>   
#include <gst/gst.h>   
#include <signal.h>   
#include <string.h>   
#include <gst/interfaces/xoverlay.h>
#include <glade/glade.h>
#include <glib.h>
#include <stdio.h>
#include <gtk/gtk.h>

#include "config.h"
#include "sys_time.h"
#include "sys_log.h"

static GtkWidget *text_widget;

static void show_text(GtkWidget *view, char* text,int size) 
{
	gchar *markup;

	if(!text || !view) {
		SYS_LOG("show_text_in_new_win error");
		return;
	}

	int str_len = strlen(text);
	markup=g_markup_printf_escaped("<span size=\"%d\" foreground=\"black\" >%s</span>",size,text);
	gtk_label_set_markup(GTK_LABEL(view),markup);
	gtk_widget_show(view);
	return;
}

static gboolean sys_show_time_text_timeout(void *data)
{
	int size = (int)data;
	time_t times=time(NULL);

	struct tm *t_m;
	t_m = gmtime(&times);
	char time_str[128] = {0};
	sprintf(time_str,"%d年%d月%d日\n %d时%d分%d秒",(t_m->tm_year+1900),(t_m->tm_mon+1),t_m->tm_mday,(t_m->tm_hour+8),t_m->tm_min,t_m->tm_sec);

	show_text(text_widget,time_str,size);
//	show_text(text_widget,ctime(&times),size);
//	gtk_label_set_text(GTK_LABEL(widget),ctime(&times));	
	return TRUE;
}

int settime(GtkWidget *widget,int data)  
{	 
	time_t times=time(NULL); 
	int size = (int)data;
	text_widget = widget;
	
	struct tm *t_m;
	t_m = gmtime(&times);
	char time_str[128] = {0};
	sprintf(time_str,"%d年%d月%d日\n %d时%d分%d秒",(t_m->tm_year+1900),(t_m->tm_mon+1),t_m->tm_mday,(t_m->tm_hour+8),t_m->tm_min,t_m->tm_sec);
	
	//show_text(GTK_LABEL(widget),(char*)ctime(&times),size);
	show_text(GTK_LABEL(widget),time_str,size);
//	gtk_label_set_text(GTK_LABEL(widget),ctime(&times));	 
	g_timeout_add(1000,(GSourceFunc)sys_show_time_text_timeout,(void*)size);
	return TRUE;  
}
	
int read_text_config_file(char* filepath,char* filepath_url, sys_config_s *target)
{
	FILE* fp = NULL;
	fp=fopen(filepath, "rw+");
	fread(target,sizeof(sys_config_s),1,fp);
	fclose(fp);


	fp=fopen(filepath_url, "rw+");
	fscanf(fp,"%s",&(target->heart_beat_url));
	fclose(fp);

	char *p = g_strrstr(target->heart_beat_url,"=");
	if(NULL != p) {
		g_stpcpy(target->client_http_hash,p+1);
	}

	printf("read_text_config_file url[%s]\n",target->heart_beat_url);
}

int save_text_config_file(char* filepath, sys_config_s *target)
{
	FILE* fp = NULL;
	fp = fopen(filepath, "rw+");
	fwrite(target,sizeof(sys_config_s),1,fp);
	fclose(fp);
}

int save_url_config_file(char* filepath, sys_config_s *target)
{
	FILE* fp = NULL;
	fp = fopen(filepath, "w+");
 // 	fwrite(target->heart_beat_url,sizeof(sys_config_s),1,fp);
    printf("save_url_config_file[%s]\n",target->heart_beat_url);
	fprintf(fp,"%s",target->heart_beat_url);
	fclose(fp);
}

unsigned long get_file_size(const char *path)  
{  
    unsigned long filesize = -1;  
    FILE *fp;  
    fp = fopen(path, "r");  
    if(fp == NULL)  
        return filesize;  
    fseek(fp, 0L, SEEK_END);  
    filesize = ftell(fp);  
    fclose(fp);  
    return filesize;  
}  
